package com.qut.graduate.busrtquery.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.poi.PoiSortType;
import com.qingmei2.library.SlideBottomLayout;
import com.qut.graduate.busrtquery.BRTQCache;
import com.qut.graduate.busrtquery.BRTQConstant;
import com.qut.graduate.busrtquery.BaseActivity;
import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.adapter.SelectPlaceSlideAdapter;
import com.qut.graduate.busrtquery.interfaces.OnItemClickListener;
import com.qut.graduate.busrtquery.listener.MyOrientationListener;
import com.qut.graduate.busrtquery.utils.baidumap.BaiduMapUtils;
import com.qut.graduate.busrtquery.utils.common.LogUtils;
import com.qut.graduate.busrtquery.utils.uiabout.KeyBoardUtils;
import com.qut.graduate.busrtquery.utils.uiabout.ScreenUtils;
import com.qut.graduate.busrtquery.utils.uiabout.StatusBarUtils;
import com.qut.graduate.busrtquery.utils.uiabout.ToastColorfulUtils;

import java.util.ArrayList;
import java.util.List;

public class SelectedPlaceActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "SelectedPlaceActivity";
    private Context mContext;
    //    private SPUtils mSPUtils;
    private boolean isFirstIn = true; // 是否是第一次定位，若是则将地图切换到定位位置
    private MyOrientationListener myOrientationListener;
    private MyLocationConfiguration.LocationMode locationMode; // 定位图层显示方式,地图SDK支持三种定位模式：NORMAL（普通态）, FOLLOWING（跟随态）, COMPASS（罗盘态）
    private LatLng mCurrentLatLng; // 当前经纬度对应位置
    private float mCurrentX; // 当前方向
    private float mCurrentAccracy; // 当前定位精度
    private double mLatitude; // 当前纬度
    private double mLongitude; // 当前经度
    private GeoCoder mSearch = null;
    private PoiSearch mPoiSearch;
    private int loadIndex = 0; // poi检索的页数索引
    private List<PoiInfo> mPoiList = new ArrayList<>(); // 存放poi检索结果
    private int mAddressType = BRTQConstant.SELECT_PLACE_TYPE_END;
    private boolean choosePlaceType = false; // 选择地点类型，true为poi检索，false为屏幕选取
    private LatLng startLatLng; // 起点经纬度
    private LatLng endLatLng; // 终点经纬度
    private Point mScreenCenterPoint;
    private BitmapDescriptor mMarkerA; // 移动屏幕起点图标
    private BitmapDescriptor mMarkerB; // 移动屏幕终点图标
    private Marker markerA; // 移动屏幕起点标记
    private Marker markerB; // 移动屏幕终点标记
    private Marker markerC; // 选择起点图标
    private Marker markerD; // 选择终点图标
    private String placeName; // 语义描述 易触科技园西南157米
    private String placeAddress; //  地址 山东省青岛市黄岛区金钱河路
    private String startPlaceName;
    private String endPlaceName;
    private BaiduMap mBaiduMap;
    private MapView mMapView = null;
    private EditText mSearchEditText;
    private LinearLayout mPlaceInfoLayout;
    private TextView mPlaceInfoName;
    private TextView mPlaceInfoAddress;
    private ImageButton mZoomIn;
    private ImageButton mZoomOut;
    private Button mPlaceSureBtn;
    private SlideBottomLayout mSlideLayout;
    private RecyclerView mPlaceRecyclerView;
    private SwipeRefreshLayout mPlaceSwipeRefreshLayout;
    private SelectPlaceSlideAdapter selectPlaceSlideAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_place;
    }

    @Override
    protected void init() {
        super.init();
        mContext = this;
//        mSPUtils = SPUtils.getInstance(getApplicationContext());
        initView();
        initEvent();
    }

    private void initEvent() {
        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(this);
        mPoiSearch = PoiSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(this);
        mMarkerA = BitmapDescriptorFactory.fromResource(R.mipmap.icon_place_start);
        mMarkerB = BitmapDescriptorFactory.fromResource(R.mipmap.icon_place_end);
        initLocation();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            mAddressType = bundle.getInt("mAddressType");
            startLatLng = bundle.getParcelable("startLatLng");
            endLatLng = bundle.getParcelable("endLatLng");
            startPlaceName = bundle.getString("startPlaceName");
            endPlaceName = bundle.getString("endPlaceName");
        } else {
            mAddressType = BRTQConstant.SELECT_PLACE_TYPE_START;
            startLatLng = mCurrentLatLng;
            endLatLng = mCurrentLatLng;
            startPlaceName = "";
            endPlaceName = "";
        }
        if (startLatLng != null && endLatLng != null) {
            addOverlay();
            initPlaceInfoLayout();
        }
        initBaiduMap();
        mSearchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!mSearchEditText.getText().toString().equals("")) {
                    BRTQCache.getInstance().setSPPoiSearchType(BRTQConstant.SELECT_PLACE_POI_TYPE_SUGGEST);
                    loadIndex = 0;
                    poiSearchNearby(loadIndex);
                } else {
                    setRefreshStop();
                    mSlideLayout.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    // 监听地图点击、长按、状态改变等
    private void initBaiduMap() {
        mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
//                if (startLatLng != null && endLatLng != null) {
                moveOverlay();
//                }
            }
        });
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                KeyBoardUtils.hideInputForce((SelectedPlaceActivity) mContext);
            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });
        mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
            @Override
            public void onMapStatusChangeStart(MapStatus mapStatus) {
            }

            @Override
            public void onMapStatusChangeStart(MapStatus status, int reason) {
            }

            @Override
            public void onMapStatusChange(MapStatus status) {
                KeyBoardUtils.hideInputForce((SelectedPlaceActivity) mContext);
                initPlaceInfoLayout();
            }

            @Override
            public void onMapStatusChangeFinish(MapStatus status) {
                if (null != mBaiduMap.getMapStatus()) {
                    if (mAddressType == BRTQConstant.SELECT_PLACE_TYPE_START) {
                        if (null == markerA) return;
                        markerA.setAnimation(BaiduMapUtils.getTransformationPoint(mScreenCenterPoint));
                        markerA.startAnimation();
                    } else {
                        if (null == markerB) return;
                        markerB.setAnimation(BaiduMapUtils.getTransformationPoint(mScreenCenterPoint));
                        markerB.startAnimation();
                    }
                }
            }
        });
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Bundle extraInfo = marker.getExtraInfo();
                if (extraInfo != null) {
                    PoiInfo info = extraInfo.getParcelable("PoiInfo");
                    int index = extraInfo.getInt("mAddressType");
                    if (info != null) {
                        placeName = info.name;
                        placeAddress = info.address;
                        mPlaceInfoName.setText(placeName);
                        mPlaceInfoAddress.setText(placeAddress);
                        if (index == 1) startLatLng = info.location;
                        else endLatLng = info.location;
                        choosePlaceType = true;
                    }
                    BaiduMapUtils.startScaleAnimation(marker);
                }
                return true;
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.select_place_back_imgBtn:
                finish();
                break;
            case R.id.select_place_sure_btn:
                backToTransferPage(mAddressType);
                break;
            case R.id.zoom_in:
                BaiduMapUtils.zoomIn(mBaiduMap, mZoomOut, mZoomIn);
                break;
            case R.id.zoom_out:
                BaiduMapUtils.zoomOut(mBaiduMap, mZoomOut, mZoomIn);
                break;
            case R.id.location_img_btn:
                BaiduMapUtils.getMyLocation(mBaiduMap, mCurrentLatLng);
                break;
        }
    }

    // 确定选择地点，返回换乘界面
    public void backToTransferPage(int mAddressType) {
        Intent intent = new Intent(SelectedPlaceActivity.this, MainActivity.class);
        Bundle bundle = new Bundle();
        if (!choosePlaceType) {
            if (mAddressType == BRTQConstant.SELECT_PLACE_TYPE_START) {
                startLatLng = mBaiduMap.getMapStatus().target;
            } else {
                endLatLng = mBaiduMap.getMapStatus().target;
            }
            choosePlaceType = false;
        }
        bundle.putInt("mAddressType", mAddressType);
        bundle.putParcelable("startLatLng", startLatLng);
        bundle.putParcelable("endLatLng", endLatLng);
        if (mAddressType == BRTQConstant.SELECT_PLACE_TYPE_START) {
            bundle.putString("startPlaceName", placeName);
            bundle.putString("endPlaceName", endPlaceName);
            LogUtils.d("----选择地点界面  更换起点  更换之前----startPlaceName: " + startPlaceName, getClass());
            LogUtils.d("----选择地点界面  更换起点  更换之后----startPlaceName: " + placeName, getClass());
            LogUtils.d("----选择地点界面  更换起点  终点不变----endPlaceName: " + endPlaceName, getClass());
        } else {
            bundle.putString("endPlaceName", placeName);
            bundle.putString("startPlaceName", startPlaceName);
            LogUtils.d("----选择地点界面  更换终点  起点不变----startPlaceName: " + startPlaceName, getClass());
            LogUtils.d("----选择地点界面  更换终点  更换之前----endPlaceName: " + endPlaceName, getClass());
            LogUtils.d("----选择地点界面  更换终点  更换之后----endPlaceName: " + placeName, getClass());
        }
        intent.putExtras(bundle);
        setResult(BRTQConstant.SELECT_PLACE_BACK_TO_TRANSFER, intent);
        finish();
    }

    public void setRefreshStart() {
        mPlaceSwipeRefreshLayout.setRefreshing(true);
    }

    public void setRefreshStop() {
        mPlaceSwipeRefreshLayout.setRefreshing(false);
    }

    public void firstRequestSuggestRecyView(List<PoiInfo> poiInfos) {
        mPoiList.clear();
        updateSuggestRecyView(poiInfos);
    }

    public void updateSuggestRecyView(List<PoiInfo> poiInfos) {
        if (poiInfos != null && poiInfos.size() > 0) {
            mPoiList.addAll(0, poiInfos);
        }
        selectPlaceSlideAdapter.notifyDataSetChanged();
    }

    // poi检索到结果时回调，将结果信息都展示到SlideBottomLayout控件中
    public void onGetPoiResult(PoiResult result) {
        if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
            if (BRTQCache.getInstance().getSPPoiSearchType() == BRTQConstant.SELECT_PLACE_POI_TYPE_SUGGEST) {
                ToastColorfulUtils.info("您查询的地点不存在，请重新输入");
                mSlideLayout.setVisibility(View.GONE);
                setRefreshStop();
                firstRequestSuggestRecyView(null);
            }
            if (BRTQCache.getInstance().getSPPoiSearchType() == BRTQConstant.SELECT_PLACE_POI_TYPE_SUGGEST_MORE) {
                ToastColorfulUtils.info("没有更多该地点的检索结果了");
                setRefreshStop();
            }
            return;
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            mSlideLayout.setVisibility(View.VISIBLE);
            mSlideLayout.show();
            List<PoiInfo> poiInfoList = new ArrayList<>();
            for (PoiInfo poiInfo : result.getAllPoi()) {
                poiInfoList.add(0, poiInfo);
            }
            if (BRTQCache.getInstance().getSPPoiSearchType() == BRTQConstant.SELECT_PLACE_POI_TYPE_SUGGEST) {
                setRefreshStop();
                firstRequestSuggestRecyView(poiInfoList);
            }
            if (BRTQCache.getInstance().getSPPoiSearchType() == BRTQConstant.SELECT_PLACE_POI_TYPE_SUGGEST_MORE) {
                setRefreshStop();
                updateSuggestRecyView(poiInfoList);
            }
        } else {
            if (BRTQCache.getInstance().getSPPoiSearchType() == BRTQConstant.SELECT_PLACE_POI_TYPE_SUGGEST_MORE)
                setRefreshStop();
        }
    }

    // 点击poi检索后的地点信息recyclerView中的item后给该item的marker设置额外信息，实现点击placeInfoLayout显示对应信息
    public void poiSearchMarkerExtraInfo(int mAddressType, int position) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("PoiInfo", mPoiList.get(position));
        bundle.putInt("mAddressType", mAddressType);
        if (mAddressType == BRTQConstant.SELECT_PLACE_TYPE_START) {
            if (markerC != null) markerC.remove();
            BitmapDescriptor mMarkerC = BitmapDescriptorFactory.fromResource(R.mipmap.icon_place_select_start);
            OverlayOptions optionsC = new MarkerOptions().position(startLatLng).icon(mMarkerC);
            markerC = (Marker) (mBaiduMap.addOverlay(optionsC));
            markerC.setExtraInfo(bundle);
        } else {
            if (markerD != null) markerD.remove();
            BitmapDescriptor mMarkerD = BitmapDescriptorFactory.fromResource(R.mipmap.icon_place_select_end);
            OverlayOptions optionsD = new MarkerOptions().position(endLatLng).icon(mMarkerD);
            markerD = (Marker) (mBaiduMap.addOverlay(optionsD));
            markerD.setExtraInfo(bundle);
        }
    }

    /**
     * 编码屏幕中心点结果回调
     * result 属性详解:
     * placeName 语义描述  易触科技园西南157米
     * placeAddress 地址 山东省青岛市黄岛区金钱河路
     * BusinessCircle 商圈  红石崖
     * AddressDetail 地址详情    street 街道 金钱河路    district 区 黄岛区
     * PoiList poi检索到的信息    Name 具体名称 易触科技园
     *
     * @param result
     */
    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            LogUtils.d("----选择地点界面  地址编码结果: 抱歉,未能找到结果", getClass());
            return;
        }
        placeAddress = result.getSematicDescription();
        if (result.getPoiList() != null)
            if (result.getPoiList().get(0).getName() != null)
                placeName = result.getPoiList().get(0).getName();
    }

    // poi检索，搜索附近的地点
    private void poiSearchNearby(int i) {
        setRefreshStart();
        String searchAddress = mSearchEditText.getText().toString().trim();
        PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption().keyword(searchAddress)
                .sortType(PoiSortType.distance_from_near_to_far).location(mBaiduMap.getMapStatus().target)
                .radius(100000).pageNum(i);
        mPoiSearch.searchNearby(nearbySearchOption);
    }

    // 添加覆盖物
    private void addOverlay() {
        mBaiduMap.clear();
        List<Overlay> mOverlayList = new ArrayList<Overlay>();
        OverlayOptions optionsA = new MarkerOptions().position(startLatLng).icon(mMarkerA).zIndex(5);
        markerA = (Marker) mBaiduMap.addOverlay(optionsA);
        mOverlayList.add(markerA);
        OverlayOptions optionsB = new MarkerOptions().position(endLatLng).icon(mMarkerB).zIndex(5);
        markerB = (Marker) mBaiduMap.addOverlay(optionsB);
        mOverlayList.add(markerB);
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (Overlay overlay : mOverlayList)
            if (overlay instanceof Marker)
                builder.include(((Marker) overlay).getPosition());
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLngBounds(builder.build()));
    }

    // 移动覆盖物，移动后上下跳动
    private void moveOverlay() {
        if (mAddressType == BRTQConstant.SELECT_PLACE_TYPE_START) {
            markerA.remove();
            startLatLng = mBaiduMap.getMapStatus().target;
            mScreenCenterPoint = mBaiduMap.getProjection().toScreenLocation(startLatLng);
            OverlayOptions optionsA = new MarkerOptions().position(startLatLng).icon(mMarkerA).perspective(true).fixedScreenPosition(mScreenCenterPoint);
            markerA = (Marker) (mBaiduMap.addOverlay(optionsA));
        } else {
            markerB.remove();
            endLatLng = mBaiduMap.getMapStatus().target;
            mScreenCenterPoint = mBaiduMap.getProjection().toScreenLocation(endLatLng);
            OverlayOptions optionsB = new MarkerOptions().position(endLatLng).icon(mMarkerB).perspective(true).fixedScreenPosition(mScreenCenterPoint);
            markerB = (Marker) (mBaiduMap.addOverlay(optionsB));
        }
    }

    // 初始化及移动屏幕时调用，显示当前的地点信息
    private void initPlaceInfoLayout() {
        mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(mBaiduMap.getMapStatus().target));
        if (placeName != null && placeAddress != null) {
            mPlaceInfoLayout.setVisibility(View.VISIBLE);
            mPlaceInfoName.setText(placeName);
            mPlaceInfoAddress.setText(placeAddress);
            if (mAddressType == BRTQConstant.SELECT_PLACE_TYPE_START) {
                mPlaceSureBtn.setText("设为起点");
                startLatLng = mBaiduMap.getMapStatus().target;
            } else {
                mPlaceSureBtn.setText("设为终点");
                endLatLng = mBaiduMap.getMapStatus().target;
            }
        }
    }

    // 定位
    private void initLocation() {
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        mBaiduMap.setMyLocationEnabled(true);
        myOrientationListener = new MyOrientationListener(this);
        myOrientationListener.setOnOrientationListener(new MyOrientationListener.OnOrientationListener() {
            @Override
            public void onOrientationChanged(float x) {
                mCurrentX = x;
                MyLocationData data = new MyLocationData.Builder()
                        .direction(mCurrentX)
                        .accuracy(mCurrentAccracy)
                        .latitude(mLatitude)
                        .longitude(mLongitude)
                        .build();
                mBaiduMap.setMyLocationData(data);
            }
        });
    }

    @Override
    // 定位改变回调，在需要的activity中重写该方法
    public void onLocationChange(BDLocation bdLocation) {
        if (bdLocation != null) {
            mLatitude = bdLocation.getLatitude();
            mLongitude = bdLocation.getLongitude();
            mCurrentLatLng = new LatLng(mLatitude, mLongitude);
            mCurrentAccracy = bdLocation.getRadius();
            MyLocationConfiguration configuration = new MyLocationConfiguration(locationMode, true, null);
            mBaiduMap.setMyLocationConfiguration(configuration);
            /*if (isFirstIn) {
                BaiduMapUtils.getMyLocation(mBaiduMap, mCurrentLatLng);
                isFirstIn = false;
            }*/
        }
    }

    private void initView() {
        StatusBarUtils.setStatusBarTransparent(this);
        mMapView = findViewById(R.id.select_place_mapView);
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setViewPadding(ScreenUtils.dp2px(10), 0, 0, 0);
        BaiduMapUtils.changeDefaultBaiDuMapView(mMapView, mBaiduMap); // 比列尺、放大、缩小按钮、图标等的显示与否
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f); // 根据给定增量缩放地图级别
        mBaiduMap.setMapStatus(msu);

        ImageButton mBackBtn = findViewById(R.id.select_place_back_imgBtn);
        mSearchEditText = findViewById(R.id.select_place_search_editText);
        ImageButton mLocationImgBtn = findViewById(R.id.location_img_btn);
        mZoomIn = findViewById(R.id.zoom_in);
        mZoomOut = findViewById(R.id.zoom_out);
        mPlaceInfoLayout = findViewById(R.id.select_place_info_layout);
        mPlaceInfoName = findViewById(R.id.select_place_info_name);
        mPlaceInfoAddress = findViewById(R.id.select_place_info_address);
        mPlaceSureBtn = findViewById(R.id.select_place_sure_btn);
        mSlideLayout = findViewById(R.id.select_place_slideLayout);
        mPlaceRecyclerView = findViewById(R.id.select_place_recyclerView);
        mPlaceSwipeRefreshLayout = findViewById(R.id.select_place_swipeRefreshLayout);

        mBackBtn.setOnClickListener(this);
        mPlaceSureBtn.setOnClickListener(this);
        mLocationImgBtn.setOnClickListener(this);
        mZoomIn.setOnClickListener(this);
        mZoomOut.setOnClickListener(this);

        mPlaceSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        mPlaceSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
        mPlaceSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                BRTQCache.getInstance().setSPPoiSearchType(BRTQConstant.SELECT_PLACE_POI_TYPE_SUGGEST_MORE);
                poiSearchNearby(++loadIndex);
                setRefreshStop();
            }
        });

        mPlaceRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        selectPlaceSlideAdapter = new SelectPlaceSlideAdapter(mContext, mPoiList, mCurrentLatLng);
        mPlaceRecyclerView.setAdapter(selectPlaceSlideAdapter);
        selectPlaceSlideAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                mSlideLayout.hide();
                if (null != mBaiduMap.getMapStatus()) {
                    mPlaceInfoName.setText(mPoiList.get(position).name);
                    mPlaceInfoAddress.setText(mPoiList.get(position).address);
                    placeName = mPoiList.get(position).name;
                    placeAddress = mPoiList.get(position).address;
                    startLatLng = mPoiList.get(position).location;
                    choosePlaceType = true;
                    poiSearchMarkerExtraInfo(mAddressType, position);
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mBaiduMap.setMyLocationEnabled(true);
        myOrientationListener.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mBaiduMap.setMyLocationEnabled(false);
        myOrientationListener.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (connection != null) {
            unbindService(connection);
        }
        mMapView.onDestroy();
        if (mSearch != null)
            mSearch.destroy();
        if (mPoiSearch != null)
            mPoiSearch.destroy();
    }

}
